﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TjZheng.Power.Business;
using TjZheng.Power.DataEntity;
using TjZheng.Framework;
using TjZheng.Base;
using System.Data.Common;
using TjZheng.CMS.Process.DataEntity;

namespace TjZheng.CMS.Process.Business
{
    public partial class UserProcess
    {
        /// <summary>
        /// 获取权限视图扩展名
        /// </summary>
        /// <param name="objUserPowerType"></param>
        /// <returns></returns>
        public string GetDataObjectParam(UserPowerType objUserPowerType)
        {
            string result = string.Empty;
            switch (objUserPowerType)
            {
                case UserPowerType.PartGroupUserPower:
                    {
                        result = "User";
                        break;
                    }
                case UserPowerType.GroupUserPower:
                    {
                        result = "GroupUser";
                        break;
                    }
                case UserPowerType.DependentUserPower:
                    {
                        result = "UserDependent";
                        break;
                    }
                case UserPowerType.GroupPower:
                    {
                        result = "Group";
                        break;
                    }
            }
            return result;
        }
        /// <summary>
        /// 获取用户权限类型
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public UserPowerType? GetUserPowerType(long userId)
        {
            BizUserInfo objBizUserInfo = new BizUserInfo();
            var objUserInfo = objBizUserInfo.GetRecord(userId);
            if (objUserInfo == null)
            {
                return null;
            }
            return (UserPowerType)Enum.Parse(typeof(UserPowerType), objUserInfo.PowerType.ToString());
        }
        public UserPowerType? ConvertoUserPowerType(int powerType)
        {
            if (powerType < 0 || powerType > 4)
            {
                return null;
            }
            return (UserPowerType)Enum.Parse(typeof(UserPowerType), powerType.ToString());
        }
        /// <summary>
        /// 用户栏目权限列表
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <returns></returns>
        public List<PowerInfo> GetUserColumnPowerInfoList(long userId, UserPowerType objUserPowerType)
        {
            BizPowerInfo objBizPowerInfo = new BizPowerInfo(GetDataObjectParam(objUserPowerType));
            var objDataTable = objBizPowerInfo.GetViewDataTable(string.Format("UserId={0} AND ParentId=1", userId));
            if (objDataTable.IsNull())
            {
                return null;
            }
            PowerInfoComparer objPowerInfoComparer = new PowerInfoComparer();
            return objBizPowerInfo.ConvertToPowerInfoList(objDataTable).Distinct(objPowerInfoComparer).OrderBy(c => c.SortIndex).ToList();
        }
        /// <summary>
        /// 角色用户列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="condition"></param>
        /// <param name="parameters"></param>
        /// <param name="sortExpression"></param>
        /// <returns></returns>
        public PageResult<UserRoleInfoUser> GetPageUserInfo(int pageIndex, int pageSize, string condition, DbParameter[] parameters, string sortExpression)
        {
            BizUserRoleInfo objBizUserRoleInfo = new BizUserRoleInfo("User");
            int recordCount = 0;
            var objDataTable = objBizUserRoleInfo.GetPageViewDataTable(pageIndex, pageSize, condition, parameters, sortExpression, out recordCount);
            PageResult<UserRoleInfoUser> objPageResult = new PageResult<UserRoleInfoUser>();
            objPageResult.rows = objBizUserRoleInfo.ConvertToUserRoleInfoUserList(objDataTable);
            objPageResult.pageIndex = pageIndex + 1;
            objPageResult.pageSize = pageSize;
            return objPageResult;
        }
        /// <summary>
        /// 用户相关权限树，并设置选中
        /// </summary>
        /// <param name="id"></param>
        /// <param name="targetId">根据具体视图来定，可以是用户组Id，也可以是用户Id</param>
        /// <param name="isContainButton"></param>
        /// <param name="objUserPowerType"></param>
        /// <returns></returns>
        public List<TreeEntity> BindCheckedPowerInfoTrees(long id, long targetId, bool isContainButton, UserPowerType objUserPowerType)
        {
            BizPowerInfo objBizPowerInfo = new BizPowerInfo();
            PowerInfo objPowerInfo = objBizPowerInfo.GetRecord(id);
            if (objPowerInfo == null)
            {
                return new List<TreeEntity>();
            }
            var objPowerInfoList = objBizPowerInfo.GetList(string.Format("IDPath like '{0}%'" + (isContainButton ? "" : " AND PowerType<>2"), objPowerInfo.IDPath)).OrderBy(c => c.SortIndex).ToList();
            if (objPowerInfoList.IsNull())
            {
                return new List<TreeEntity>();
            }
            objBizPowerInfo = new BizPowerInfo(this.GetDataObjectParam(objUserPowerType));
            string userCondition = "UserId={0}";
            if (objUserPowerType == UserPowerType.GroupPower)
            {
                userCondition = "GroupId={0}";
            }
            var objDataTable = objBizPowerInfo.GetViewDataTable(string.Format(userCondition + " AND IDPath like '{1}%'" + (isContainButton ? "" : " AND PowerType<>2"), targetId, objPowerInfo.IDPath));
            var objCheckedPowerInfoList = objBizPowerInfo.ConvertToPowerInfoList(objDataTable);
            PowerInfoComparer objPowerInfoComparer = new PowerInfoComparer();
            return objPowerInfoList.Distinct(objPowerInfoComparer).OrderBy(c => c.SortIndex).ToList().ConvertToCheckedTreeList(id, objCheckedPowerInfoList) ?? new List<TreeEntity>();
        }
        /// <summary>
        /// 继承用户组部分权限树，并设置选中
        /// </summary>
        /// <param name="id"></param>
        /// <param name="userId"></param>
        /// <param name="isContainButton"></param>
        /// <returns></returns>
        public List<TreeEntity> BindCheckedGroupUserPowerInfoTrees(long id, long userId, bool isContainButton)
        {
            BizUserRoleInfo objBizUserRoleInfo = new BizUserRoleInfo();
            var objGroupIds = objBizUserRoleInfo.GetFieldList<long>("UserId=" + userId, "GroupId");
            if (objGroupIds.IsNull())
            {
                return new List<TreeEntity>();
            }
            BizPowerInfo objBizPowerInfo = new BizPowerInfo("Group");
            var objDataTable = objBizPowerInfo.GetViewDataTable(string.Format("GroupId in({0})" + (isContainButton ? "" : " AND PowerType<>2"), objGroupIds.JoinComma()));
            var objPowerInfoList = objBizPowerInfo.ConvertToPowerInfoList(objDataTable);
            if (objPowerInfoList.IsNull())
            {
                return new List<TreeEntity>();
            }
            objBizPowerInfo = new BizPowerInfo("User");
            objDataTable = objBizPowerInfo.GetViewDataTable(string.Format("UserId={0}" + (isContainButton ? "" : " AND PowerType<>2"), userId));
            var objCheckedPowerInfoList = objBizPowerInfo.ConvertToPowerInfoList(objDataTable);
            PowerInfoComparer objPowerInfoComparer = new PowerInfoComparer();
            return objPowerInfoList.Distinct(objPowerInfoComparer).OrderBy(c => c.SortIndex).ToList().ConvertToCheckedTreeList(id, objCheckedPowerInfoList) ?? new List<TreeEntity>();
        }
        /// <summary>
        /// 用户相关权限树
        /// </summary>
        /// <param name="id"></param>
        /// <param name="targetId">根据具体视图来定，可以是用户组Id，也可以是用户Id</param>
        /// <param name="isContainButton"></param>
        /// <param name="objUserPowerType"></param>
        /// <returns></returns>
        public List<TreeEntity> BindPowerInfoTrees(long id, long targetId, bool isContainButton, UserPowerType objUserPowerType)
        {
            BizPowerInfo objBizPowerInfo = new BizPowerInfo();
            PowerInfo objPowerInfo = objBizPowerInfo.GetRecord(id);
            if (objPowerInfo == null)
            {
                return new List<TreeEntity>();
            }
            objBizPowerInfo = new BizPowerInfo(this.GetDataObjectParam(objUserPowerType));
            string userCondition = "UserId={0}";
            if (objUserPowerType == UserPowerType.GroupPower)
            {
                userCondition = "GroupId={0}";
            }
            var objDataTable = objBizPowerInfo.GetViewDataTable(string.Format(userCondition + " AND IDPath like '{1}%'" + (isContainButton ? "" : " AND PowerType<>2"), targetId, objPowerInfo.IDPath));
            if (objDataTable.IsNull())
            {
                return new List<TreeEntity>();
            }
            PowerInfoComparer objPowerInfoComparer = new PowerInfoComparer();
            return objBizPowerInfo.ConvertToPowerInfoList(objDataTable).Distinct(objPowerInfoComparer).OrderBy(c => c.SortIndex).ToList().ConvertToTreeList(id) ?? new List<TreeEntity>();
        }
        /// <summary>
        /// 除隐藏权限的用户相关权限树
        /// </summary>
        /// <param name="id"></param>
        /// <param name="targetId"></param>
        /// <param name="isContainButton"></param>
        /// <param name="objUserPowerType"></param>
        /// <returns></returns>
        public List<TreeEntity> BindPowerInfoTreesWithoutUnDisplay(long id, long targetId, bool isContainButton, UserPowerType objUserPowerType)
        {
            BizPowerInfo objBizPowerInfo = new BizPowerInfo();
            PowerInfo objPowerInfo = objBizPowerInfo.GetRecord(id);
            if (objPowerInfo == null)
            {
                return new List<TreeEntity>();
            }
            objBizPowerInfo = new BizPowerInfo(GetDataObjectParam(objUserPowerType));
            string userCondition = "UserId={0}";
            if (objUserPowerType == UserPowerType.GroupPower)
            {
                userCondition = "GroupId={0}";
            }
            var objDataTable = objBizPowerInfo.GetViewDataTable(string.Format(userCondition + " AND IDPath like '{1}%' AND IsDisplay=1" + (isContainButton ? "" : " AND PowerType<>2"), targetId, objPowerInfo.IDPath));
            if (objDataTable.IsNull())
            {
                return new List<TreeEntity>();
            }
            PowerInfoComparer objPowerInfoComparer = new PowerInfoComparer();
            return objBizPowerInfo.ConvertToPowerInfoList(objDataTable).Distinct(objPowerInfoComparer).OrderBy(c => c.SortIndex).ToList().ConvertToTreeList(id) ?? new List<TreeEntity>();
        }
        /// <summary>
        /// 除根节点的用户权限列表树
        /// </summary>
        /// <param name="id"></param>
        /// <param name="targetId"></param>
        /// <param name="isContainButton"></param>
        /// <param name="objUserPowerType"></param>
        /// <returns></returns>
        public List<TreeEntity> BindPowerInfoTreeList(long id, long targetId, bool isContainButton, UserPowerType objUserPowerType)
        {
            BizPowerInfo objBizPowerInfo = new BizPowerInfo();
            PowerInfo objPowerInfo = objBizPowerInfo.GetRecord(id);
            if (objPowerInfo == null)
            {
                return new List<TreeEntity>();
            }
            objBizPowerInfo = new BizPowerInfo(GetDataObjectParam(objUserPowerType));
            string userCondition = "UserId={0}";
            if (objUserPowerType == UserPowerType.GroupPower)
            {
                userCondition = "GroupId={0}";
            }
            var objDataTable = objBizPowerInfo.GetViewDataTable(string.Format(userCondition + " AND IDPath like '{1}%'" + (isContainButton ? "" : " AND PowerType<>2"), targetId, objPowerInfo.IDPath));
            if (objDataTable.IsNull())
            {
                return new List<TreeEntity>();
            }
            PowerInfoComparer objPowerInfoComparer = new PowerInfoComparer();
            return objBizPowerInfo.ConvertToPowerInfoList(objDataTable).Distinct(objPowerInfoComparer).OrderBy(c => c.SortIndex).ToList().ConvertToTreeListExceptRoot(id) ?? new List<TreeEntity>();
        }
        /// <summary>
        /// 除根节点和隐藏权限的用户权限树
        /// </summary>
        /// <param name="id"></param>
        /// <param name="targetId"></param>
        /// <param name="isContainButton"></param>
        /// <param name="objUserPowerType"></param>
        /// <returns></returns>
        public List<TreeEntity> BindPowerInfoTreeListWithoutUnDisplay(long id, long targetId, bool isContainButton, UserPowerType objUserPowerType)
        {
            BizPowerInfo objBizPowerInfo = new BizPowerInfo();
            PowerInfo objPowerInfo = objBizPowerInfo.GetRecord(id);
            if (objPowerInfo == null)
            {
                return new List<TreeEntity>();
            }
            objBizPowerInfo = new BizPowerInfo(GetDataObjectParam(objUserPowerType));
            string userCondition = "UserId={0}";
            if (objUserPowerType == UserPowerType.GroupPower)
            {
                userCondition = "GroupId={0}";
            }
            var objDataTable = objBizPowerInfo.GetViewDataTable(string.Format(userCondition + " AND IDPath like '{1}%' AND IsDisplay=1" + (isContainButton ? "" : " AND PowerType<>2"), targetId, objPowerInfo.IDPath));
            if (objDataTable.IsNull())
            {
                return new List<TreeEntity>();
            }
            PowerInfoComparer objPowerInfoComparer = new PowerInfoComparer();
            return objBizPowerInfo.ConvertToPowerInfoList(objDataTable).Distinct(objPowerInfoComparer).OrderBy(c => c.SortIndex).ToList().ConvertToTreeListExceptRoot(id) ?? new List<TreeEntity>();
        }
        /// <summary>
        /// 除父节点和按钮的用户子权限列表
        /// </summary>
        /// <param name="id"></param>
        /// <param name="targetId"></param>
        /// <param name="isContainUnDisplay"></param>
        /// <param name="objUserPowerType"></param>
        /// <returns></returns>
        public List<TreeEntity> BindPowerInfoChildrenWithoutButton(long id, long targetId, bool isContainUnDisplay, UserPowerType objUserPowerType)
        {
            BizPowerInfo objBizPowerInfo = new BizPowerInfo();
            PowerInfo objPowerInfo = objBizPowerInfo.GetRecord(id);
            if (objPowerInfo == null)
            {
                return new List<TreeEntity>();
            }
            objBizPowerInfo = new BizPowerInfo(GetDataObjectParam(objUserPowerType));
            string userCondition = "UserId={0}";
            if (objUserPowerType == UserPowerType.GroupPower)
            {
                userCondition = "GroupId={0}";
            }
            var objDataTable = objBizPowerInfo.GetViewDataTable(string.Format(userCondition + " AND ParentId={1} AND IDPath like '{2}%' AND PowerType<>2" + (isContainUnDisplay ? "" : " AND IsDisplay=1"), targetId, objPowerInfo.Id, objPowerInfo.IDPath));
            if (objDataTable.IsNull())
            {
                return new List<TreeEntity>();
            }
            PowerInfoComparer objPowerInfoComparer = new PowerInfoComparer();
            return objBizPowerInfo.ConvertToPowerInfoList(objDataTable).Distinct(objPowerInfoComparer).OrderBy(c => c.SortIndex).ToList().ConvertToTreeListExceptRoot(id) ?? new List<TreeEntity>();
        }
    }
}
